﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using VitCode.Web.UI.WebControls;
using VitCode.Castle.DataAccess;

namespace CRS.Web.BaseData
{
    public partial class OrdersEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            
            pgList.DataSourceDelegate=new VitCode.Web.UI.PaginationGrid.GridDataSourceDelegate(BindData);
            if (!IsPostBack)
            {
                Session["m_SelectedGoods"] = null;
                Session["_DataSource"] = null;
                BindDrpList();
                InitInfo();
                Session["SelectOrder"] = null;
                pgList.BindGrid();
            }
            if (Session["m_SelectedGoods"] != null)
            {
                BindSelectedData();
                pgList.BindGrid();
            }
        }

        private void InitInfo()
        {
            if (Session["SelectOrder"] != null)
            {
                var selectInfo = (DataRow[])Session["SelectOrder"];
                lblBillNo.Text = selectInfo[0]["单号"].ToString();
                lblCreateUser.Text = selectInfo[0]["创建人"].ToString();
                lblCreateDate.Text= selectInfo[0]["创建时间"].ToString();
                TextBox1.Text = selectInfo[0]["备注"].ToString();
            }
            else
            {
                lblBillNo.Text = "VIC_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +
                                 DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +
                                 DateTime.Now.Minute.ToString();
                lblCreateUser.Text = Server.UrlDecode(GenericContext.CurrUserName);
                lblCreateDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        protected void tabMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            if(tabMenu.SelectedValue=="0")
            {
                BillInfo.Visible = true;
                GoodsInfoDiv.Visible = false;
            }
            else
            {
                BillInfo.Visible = false;
                GoodsInfoDiv.Visible = true;
            }
        }

        private DataTable _dataSource;

        private DataTable DataSource
        {
            get
            {
                if (Session["_DataSource"] != null)
                    _dataSource = (DataTable) Session["_DataSource"];
                else
                {
                    _dataSource = InitData();
                    Session["_DataSource"] = _dataSource;
                }
                return _dataSource;
            }
            set { Session["_DataSource"] = value; }
        }

        private object BindData()
        {
            return DataSource;
        }

        private DataTable InitData()
        {
            var table = new DataTable();
            table.Columns.Add("条码");
            table.Columns.Add("品名");
            table.Columns.Add("规格");
            table.Columns.Add("数量");
            table.Columns.Add("单位");
            return table;
        }

        private void BindSelectedData()
        {
            if(Session["m_SelectedGoods"]!=null)
            {
                var selected = (string[]) Session["m_SelectedGoods"];
                var table = DataSource.Clone();
                foreach (var s in selected)
                {
                    var test = s.Split(new char[] {','});
                    var row = table.NewRow();
                    row["条码"] = test[0];
                    row["品名"] = test[1];
                    row["规格"] = test[2];
                    row["数量"] = test[3];
                    row["单位"] = test[4];
                    table.Rows.Add(row);
                }
                DataSource = table;
                Session["m_SelectedGoods"] = null;
            }
        }

        private void BindDrpList()
        {
            drpList.Items.Add("新建");
            drpList.Items.Add("配货中");
            drpList.Items.Add("已到货");
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            var window=new Window(this,"OrdersEdit");
            window.ShowModalDialog("../Selected/SelGoodsList.aspx");
        }

        protected void pgList_OnGridItemSelect(object sender, VitCode.Web.UI.PaginationGridItemSelectArgs e)
        {
            var selectRow = DataSource.Select("条码='" + pgList.SelectedDataKeyValue + "'");
            if(selectRow.Length>0)
            {
                lblBarcodeInfo.Text = selectRow[0]["条码"].ToString();
                lblNameInfo.Text = selectRow[0]["品名"].ToString();
                lblSpecificationInfo.Text = selectRow[0]["规格"].ToString();
                lblUnitInfo.Text = selectRow[0]["单位"].ToString();
                txtAmount.Value = selectRow[0]["数量"].ToString();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(lblBarcodeInfo.Text.Trim()))
            {
                //DataSource.PrimaryKey = new DataColumn[]{DataSource.Columns["条码"]};
                //DataSource.Rows.Find(lblBarcodeInfo.Text.Trim())["数量"] = txtAmount.Value;
                DataSource.Select("条码='" + lblBarcodeInfo.Text.Trim() + "'")[0]["数量"]=txtAmount.Value;
                //row[0]["数量"] = txtAmount.Value;
                //DataSource.Rows.Add(row[0]);
            }
        }
    }
}
